Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

143
Vistas
TypeError "cannot set properties of undefined" when Shuffling Array from Module

when I try to shuffle an array I get a TypeError: "cannot set properties of undefined (setting '10')" where 10 is the value of randomNmbrArr[0]. I have 2 other functions that I copied this code from that do work without any problems. I cant find the difference between the working code and the part that doesn't.

let nmr always logs a valid number between the range of 10 and 72. But for some reason randomNmbrArr[nmr] is undefined. Any idea's?

p.s: The code is running from a module but gives the same error in the main file. The array and function are declared before they are called in any way.

working code:

let unicodeArr = [];

for (let i = 0; i < 65537; i++) {
  unicodeArr.push(i);
};

function randomNumber2() {
  let o = 0;
  let nmr2;
  while (++o < 65536) {
    nmr2 = Math.floor(Math.random() * (65536 + 1));
    [unicodeArr[nmr2], unicodeArr[0]] = [unicodeArr[0], unicodeArr[nmr2]];
  };
  return unicodeArr[0];
};

Code that does not work:

let randomNmbrArr = [];

for (let i = 10; i < 100; i++) {
  randomNmbrArr.push(i);
}
console.log(randomNmbrArr)

function makeRandomNmbrs() {
  let k = 0;
  let nmr;
  while (++k < 62) {
    nmr = Math.floor(Math.random() * (62 + 1)) + 10;
    console.log(nmr);
    [randomNmbrArr[nmr], randomNmbrArr[0]] = [randomNmbrArr[0], randomNmbrArr[nmr]]; //TypeError at this line
    console.log(randomNmbrArr)
  }

}

about 4 years ago · Juan Pablo Isaza
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda